package com.springboot.demo.mapper;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.springboot.demo.entity.HistoryAlarm;
import com.springboot.demo.entity.HistoryData;
import io.swagger.annotations.ApiModelProperty;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.DeleteMapping;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author wubc683
 * @desc
 * @date 2021-12-23 16:15
 */
public interface AlarmMapper {
    @Select("<script> select ha.id as id,v.name as varName,v.dataType as dataType,ha.value as " +
            "value,d.deviceName as " +
            "deviceName,s.name as sceneName,ha.productTime as productTime from " +
            "web_history_data ha left join web_scene s on s.id = ha.sceneId left join web_variable v on v.id = ha" +
            ".varId left join web_connection c on c.sceneId = v.sceneId and c.connectNo = v.connectNo left join " +
            "web_device d on c.deviceId = d" + ".id " + "where ha.ownerId = #{userId} " +
            "<if test='sceneId !=null '> and ha.sceneId = #{sceneId} </if>" +
            "<if test='search !=null and search !=\"\"'> and concat(ifnull(v.name,''),ifnull(v.dataType,''),ifnull(ha" +
            ".value,''),ifnull(d.deviceName,'')," +
            "ifnull(s.name,'')," + "ifnull(ha.productTime,''))  like concat('%', #{search},'%') </if> " +
            "<if test='startDate !=null '> and productTime <![CDATA[>=]]> #{startDate} </if>" +
            "<if test='endDate !=null '> and productTime <![CDATA[<=]]> #{endDate} </if>" +
            "<if test='sort !=null and sort != \"\"'> order by ${sort} </if>" +
            "<if test='sort ==null or sort == \"\"'> order by productTime desc </if>" +
            "</script>")
    List<HistoryData> getHistoryDataInfo(Long userId, Long sceneId, Date startDate, Date endDate, String sort,
                                         String search);

    @Select("<script> select hd.id as id,v.name as varName,v.dataType as dataType,hd.value as " +
            "value,d.deviceName as deviceName,s.name as sceneName,hd.productTime as productTime ," +
            "hd.alarmType as alarmType,hd.alarmText as alarmText from " +
            "web_history_alarm hd left join web_scene s on s.id = hd.sceneId left join web_variable v on v.id = hd" +
            ".varId left join web_connection c on c.sceneId = v.sceneId and c.connectNo = v.connectNo left join " +
            "web_device d" + " on c.deviceId = d.id " + "where hd.ownerId = #{userId} " +
            "<if test='sceneId !=null '> and hd.sceneId = #{sceneId} </if>" +
            "<if test='search !=null and search !=\"\"'> and concat(ifnull(v.name,''),ifnull(v.dataType,''),ifnull(hd" +
            ".value,''),ifnull(d.deviceName,''),ifnull(alarmText,'')," +
            "ifnull(s.name,'')," + "ifnull(hd.productTime,''))  like concat('%', #{search},'%') </if> " +
            "<if test='startDate !=null '> and productTime <![CDATA[>=]]> #{startDate} </if>" +
            "<if test='endDate !=null '> and productTime <![CDATA[<=]]> #{endDate} </if>" +
            "<if test='sort !=null and sort != \"\"'> order by ${sort} </if>" +
            "<if test='sort ==null or sort == \"\"'> order by productTime desc </if>" +
            "</script>")
    List<HistoryAlarm> getHistoryAlarmInfo(Long userId, Long sceneId, Date startDate, Date endDate, String sort,
                                           String search);

    @Insert("<script> insert into web_history_data(varId,value,sceneId,productTime,ownerId) values" +
            " <foreach item='item' collection='data' separator=','> (#{item.varId},#{item.value},#{item" +
            ".sceneId},#{item.productTime},#{item.ownerId}) </foreach> </script>")
    void saveSceneHistoryData(List<HistoryData> data);

    @Insert("<script> insert into web_history_alarm(varId,value,sceneId,productTime,alarmType,alarmText,ownerId) " +
            "values" + " <foreach item='item' collection='alarm' separator=','> (#{item.varId},#{item.value},#{item" +
            ".sceneId},#{item.productTime},#{item.alarmType},#{item.alarmText},#{item.ownerId}) </foreach> </script>")
    void saveSceneHistoryAlarm(List<HistoryAlarm> alarm);

    @Select("select sum(record.num) from (select count(1) as num from web_history_data hd left join web_user u " +
            " on u.id = hd.ownerId where " + " u.id = #{userId} or (u.parentId = #{userId} and deleteFlag = '') " +
            " union all " + "select count(1) as num from" +
            " web_history_alarm ha left join web_user u on u.id = ha.ownerId where u.id = #{userId} or (u.parentId = " +
            "#{userId}) and deleteFlag = '') as record")
    Integer getHistoryRecordCount(Long userId);

    @Delete("<script> delete from web_history_alarm where ownerId = #{userId} " +
            " <if test='startDate !=null '> and #{startDate} &gt;= productTime </if> " +
            " <if test='endDate !=null '> and #{endDate} &lt;= productTime </if> </script>")
    void deleteHistoryAlarm(Long userId, Date startDate, Date endDate);

    @Delete("<script> delete from web_history_data where ownerId = #{userId} " +
            " <if test='startDate !=null '> and #{startDate} &gt;= productTime </if> " +
            " <if test='endDate !=null '> and #{endDate} &lt;= productTime </if> </script>")
    void deleteHistoryData(Long userId, Date startDate, Date endDate);

    @Delete(" delete web_history_data ,web_history_alarm from " +
            " web_history_data left join " +
            "web_history_alarm on web_history_data.sceneId = web_history_alarm.sceneId and " +
            " web_history_data.varId = web_history_alarm.varId and " +
            "web_history_data" + ".productTime = web_history_alarm.productTime " +
            " inner join web_user on web_history_data.ownerId = web_user.id ," +
            " ((select productTime from  web_history_data inner join web_user on web_history_data.ownerId = " +
            "web_user.id and (web_user.id = #{userId} or web_user.parentId = #{userId})) union all (select productTime " +
            "from web_history_alarm inner join web_user on web_history_alarm.ownerId = web_user.id and (web_user.id =" +
            "#{userId} or web_user.parentId = #{userId})) order by productTime asc limit #{count},1) as tmp " +
            " where (web_user.id = #{userId} or web_user.parentId = #{userId}) and web_history_data.productTime < tmp" +
            ".productTime ")
    void deleteOldDataFromHistoryDataAndAlarm(Long userId, int count);

}
